﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Windows;
using System.Collections.ObjectModel;
using System.Collections;
namespace BallData
{
    // 显示所有公司的逻辑
    public class ShowAllCompanyLogic : IListenCompany
    {
        private MainUIControl uicontrol;

        public MainUIControl Uicontrol
        {
            get { return uicontrol; }
            set { uicontrol = value; }
        }
        public void OnListenCompany(CompanyItem ci, MainUIControl ui)
        {

            
            bool bfind = false;
            foreach (RadioButton rab in ui.getCompanysTree().Children)
            {
                if (rab.Content as String == ci.cnName)
                {
                    bfind = true;
                }
            }
            if (!bfind)
            {
                RadioButton rab = Uicontrol.NewRadioButton();
                
                rab.Content = ci.cnName;
                rab.Click += new RoutedEventHandler(Rab_Selected);
                //tv.Selected += new RoutedEventHandler(tv_Selected);

                rab.DataContext = ci.cId;
                
                foreach (Int32 id in OddsUntil.MainCompanyInWorld)
                {
                    if (Int32.Parse(ci.cId) == id)
                    {
                        
                        ui.getCompanysTree().Children.Insert(0, rab);
                    }
                }
                //if(!binsert)
                //    ui.getCompanysTree().Items.Add(tv);
            }


            
        }

        void Rab_Selected(object sender, RoutedEventArgs e)
        {
            MatchData matchdataobj = Uicontrol.getMatchData();
            if (sender is RadioButton)
            {
                RadioButton rab = sender as RadioButton;
                TabItem tb = Uicontrol.getLeagueTab().SelectedItem as TabItem;

                if (tb == null)
                    return;

                String lId = tb.DataContext as String;

                if (lId == null)
                    return;

                if (rab.DataContext != null && rab.DataContext is String)
                {
                    foreach (String ck in matchdataobj.CompanyList.Keys)
                    {
                        if (ck == rab.DataContext as String)
                        {
                            CompanyItem ci = matchdataobj.CompanyList[ck] as CompanyItem;
                            ObservableCollection<MatchListDataTemple> matchlist = OddsUntil.LeagueViewTemple[lId] as ObservableCollection<MatchListDataTemple>;

                            if (lId == OddsUntil.WinLoseCaiPiaoName)// 胜负彩票
                            {
                                ObservableCollection<MatchListDataTemple> replace = new ObservableCollection<MatchListDataTemple>();
                                foreach (MatchListDataTemple mdtp in matchlist)
                                {
                                    MatchItem matchitem = Uicontrol.getMatchData().getMatchItem(mdtp.mId);

                                    MatchListDataTemple matchlistdatatemple = OddsUntil.MathViewTemple[OddsUntil.GetMathViewKey(matchitem.mId, ci.cId)] as MatchListDataTemple;

                                    if (matchlistdatatemple == null)
                                    {
                                        CompanyOdds sh = Uicontrol.getMatchData().companymatchsodds[ck] as CompanyOdds;

                                        LeagueItem leagueitem = matchdataobj.LeagueList[matchitem.lId] as LeagueItem;

                                        OddsInfo odds_asia = null, odds_eupro = null, odds_bigsmall = null;

                                        if (sh == null)
                                        {
                                            Uicontrol.getLogBoxList().Add("从服务器获取:" + ci.cnName);

                                            Gooooal_NetDataHelper.getoddsbycompanyInday(ck);

                                            sh = Uicontrol.getMatchData().companymatchsodds[ck] as CompanyOdds;
                                        }


                                        if (sh != null)
                                        {
                                            Hashtable mList = sh.o_data.MatchList[matchitem.mId] as Hashtable;

                                            OddsUntil.GetDataBymList(ref odds_asia, ref odds_eupro, ref odds_bigsmall, ref mList);

                                        }
                                        else
                                        {
                                            continue;
                                        }

                                        matchlistdatatemple = new MatchListDataTemple
                                        {
                                            LeagueName = leagueitem.cnName,
                                            LeagueColor = leagueitem.color,
                                            MatchHost = matchitem.t1CnName,
                                            MatchVisit = matchitem.t2CnName,
                                            CompanyName = ci.cnName,
                                            CompanyColor = OddsUntil.White,
                                            Odds_a_host = odds_asia.host,
                                            Odds_a_vs = OddsUntil.GetVsName(odds_asia.vs, "tr"),
                                            Odds_a_visit = odds_asia.visit,

                                            Odds_e_host = odds_eupro.host,
                                            Odds_e_vs = odds_eupro.vs,
                                            Odds_e_visit = odds_eupro.visit,

                                            Odds_o_host = odds_bigsmall.host,
                                            Odds_o_vs = OddsUntil.GetTotalName(odds_bigsmall.vs),
                                            Odds_o_visit = odds_bigsmall.visit,
                                            mId = matchitem.mId,
                                            MatchKoTime = matchitem.koDay,

                                            Odds_a_host_color = OddsUntil.White,
                                            Odds_a_visit_color = OddsUntil.White,

                                            Odds_o_host_color = OddsUntil.White,
                                            Odds_o_visit_color = OddsUntil.White,

                                            Odds_e_host_color = OddsUntil.White,
                                            Odds_e_visit_color = OddsUntil.White,

                                            Odds_a_chgtime = odds_asia.chgTime,
                                            Odds_e_chgtime = odds_eupro.chgTime,
                                            Odds_o_chgtime = odds_bigsmall.chgTime,
                                        };

                                        OddsUntil.MathViewTemple[OddsUntil.GetMathViewKey(matchitem.mId, ci.cId)] = matchlistdatatemple;

                                        replace.Add(matchlistdatatemple);

                                    }
                                    else
                                    {
                                        replace.Add(matchlistdatatemple);
                                    
                                    }
                                    
                                
                                }
                                matchlist = replace;
                                OddsUntil.LeagueViewTemple[lId] = replace;

                            }
                            else
                            {
                                matchlist.Clear();

                                foreach (MatchItem matchitem in matchdataobj.Matchs.Values)
                                {
                                    if (matchitem.lId == lId)
                                    {
                                        MatchListDataTemple matchlistdatatemple = OddsUntil.MathViewTemple[OddsUntil.GetMathViewKey(matchitem.mId, ci.cId)] as MatchListDataTemple;

                                        if (matchlistdatatemple != null)
                                        {
                                            matchlist.Add(matchlistdatatemple);
                                        }
                                        else
                                        {
                                            LeagueItem leagueitem = matchdataobj.LeagueList[lId] as LeagueItem;
                                            CompanyOdds sh = Uicontrol.getMatchData().companymatchsodds[ck] as CompanyOdds;
                                            OddsInfo odds_asia = null, odds_eupro = null, odds_bigsmall = null;

                                            if (sh == null)
                                            {
                                                Uicontrol.getLogBoxList().Add("从服务器获取:" + ci.cnName);

                                                Gooooal_NetDataHelper.getoddsbycompanyInday(ck);

                                                sh = Uicontrol.getMatchData().companymatchsodds[ck] as CompanyOdds;
                                            }


                                            if (sh != null)
                                            {
                                                Hashtable mList = sh.o_data.MatchList[matchitem.mId] as Hashtable;

                                                OddsUntil.GetDataBymList(ref odds_asia, ref odds_eupro, ref odds_bigsmall, ref mList);

                                            }
                                            else
                                            {
                                                continue;
                                            }

                                            matchlistdatatemple = new MatchListDataTemple
                                            {
                                                LeagueName = leagueitem.cnName,
                                                LeagueColor = leagueitem.color,
                                                MatchHost = matchitem.t1CnName,
                                                MatchVisit = matchitem.t2CnName,
                                                CompanyName = ci.cnName,
                                                CompanyColor = OddsUntil.White,
                                                Odds_a_host = odds_asia.host,
                                                Odds_a_vs = OddsUntil.GetVsName(odds_asia.vs, "tr"),
                                                Odds_a_visit = odds_asia.visit,

                                                Odds_e_host = odds_eupro.host,
                                                Odds_e_vs = odds_eupro.vs,
                                                Odds_e_visit = odds_eupro.visit,

                                                Odds_o_host = odds_bigsmall.host,
                                                Odds_o_vs = OddsUntil.GetTotalName(odds_bigsmall.vs),
                                                Odds_o_visit = odds_bigsmall.visit,
                                                mId = matchitem.mId,
                                                MatchKoTime = matchitem.koDay,

                                                Odds_a_host_color = OddsUntil.White,
                                                Odds_a_visit_color = OddsUntil.White,

                                                Odds_o_host_color = OddsUntil.White,
                                                Odds_o_visit_color = OddsUntil.White,

                                                Odds_e_host_color = OddsUntil.White,
                                                Odds_e_visit_color = OddsUntil.White,

                                                Odds_a_chgtime = odds_asia.chgTime,
                                                Odds_e_chgtime = odds_eupro.chgTime,
                                                Odds_o_chgtime = odds_bigsmall.chgTime,
                                            };

                                            OddsUntil.MathViewTemple[OddsUntil.GetMathViewKey(matchitem.mId, ci.cId)] = matchlistdatatemple;
                                            matchlist.Add(matchlistdatatemple);
                                        }


                                    }
                                }
                            }
                           



                            if (tb != null)
                            {
                                ListView listviewother = tb.Content as ListView;
                                listviewother.ItemsSource = matchlist;
                            }

                        }


                    }
                }
            }
        }
        void tv_Selected(object sender, RoutedEventArgs e)
        {
            MatchData matchdataobj = Uicontrol.getMatchData();
            if (sender is TreeViewItem)
            {
                TreeViewItem tv = sender as TreeViewItem;
                TabItem tb = Uicontrol.getLeagueTab().SelectedItem as TabItem;

                if (tb == null)
                    return;

                String lId = tb.DataContext as String;

                if (tv.DataContext != null && tv.DataContext is String)
                {
                    foreach (String ck in matchdataobj.CompanyList.Keys)
                    {
                        if (ck == tv.DataContext as String)
                        {
                            CompanyItem ci = matchdataobj.CompanyList[ck] as CompanyItem;
                            ObservableCollection<MatchListDataTemple> matchlist = matchlist = OddsUntil.LeagueViewTemple[lId] as ObservableCollection<MatchListDataTemple>;

                            matchlist.Clear();

                            foreach (MatchItem matchitem in matchdataobj.Matchs.Values)
                            {
                                if (matchitem.lId == lId)
                                {
                                    MatchListDataTemple matchlistdatatemple = OddsUntil.MathViewTemple[OddsUntil.GetMathViewKey(matchitem.mId, ci.cId)] as MatchListDataTemple;

                                    if (matchlistdatatemple != null)
                                    {
                                        matchlist.Add(matchlistdatatemple);
                                    }
                                    else
                                    {
                                        LeagueItem leagueitem = matchdataobj.LeagueList[lId] as LeagueItem;
                                        CompanyOdds sh = Uicontrol.getMatchData().companymatchsodds[ck] as CompanyOdds;
                                        OddsInfo odds_asia = null, odds_eupro = null, odds_bigsmall = null;

                                        if (sh == null)
                                        {
                                            Uicontrol.getLogBoxList().Add("从服务器获取:" + ci.cnName);

                                            Gooooal_NetDataHelper.getoddsbycompanyInday(ck);

                                            sh = Uicontrol.getMatchData().companymatchsodds[ck] as CompanyOdds;
                                        }


                                        if (sh != null)
                                        {
                                            Hashtable mList = sh.o_data.MatchList[matchitem.mId] as Hashtable;

                                            OddsUntil.GetDataBymList(ref odds_asia, ref odds_eupro, ref odds_bigsmall, ref mList);

                                        }
                                        else
                                        {
                                            continue;
                                        }

                                        matchlistdatatemple = new MatchListDataTemple
                                        {
                                            LeagueName = leagueitem.cnName,
                                            LeagueColor = leagueitem.color,
                                            MatchHost = matchitem.t1CnName,
                                            MatchVisit = matchitem.t2CnName,
                                            CompanyName = ci.cnName,
                                            CompanyColor = OddsUntil.White,
                                            Odds_a_host = odds_asia.host,
                                            Odds_a_vs = OddsUntil.GetVsName(odds_asia.vs, "tr"),
                                            Odds_a_visit = odds_asia.visit,

                                            Odds_e_host = odds_eupro.host,
                                            Odds_e_vs = odds_eupro.vs,
                                            Odds_e_visit = odds_eupro.visit,

                                            Odds_o_host = odds_bigsmall.host,
                                            Odds_o_vs = OddsUntil.GetTotalName(odds_bigsmall.vs),
                                            Odds_o_visit = odds_bigsmall.visit,
                                            mId = matchitem.mId,
                                            MatchKoTime = matchitem.koDay,

                                            Odds_a_host_color = OddsUntil.White,
                                            Odds_a_visit_color = OddsUntil.White,

                                            Odds_o_host_color = OddsUntil.White,
                                            Odds_o_visit_color = OddsUntil.White,

                                            Odds_e_host_color = OddsUntil.White,
                                            Odds_e_visit_color = OddsUntil.White,

                                            Odds_a_chgtime = odds_asia.chgTime,
                                            Odds_e_chgtime = odds_eupro.chgTime,
                                            Odds_o_chgtime = odds_bigsmall.chgTime,
                                        };

                                        OddsUntil.MathViewTemple[OddsUntil.GetMathViewKey(matchitem.mId, ci.cId)] = matchlistdatatemple;
                                        matchlist.Add(matchlistdatatemple);
                                    }


                                }
                            }



                            if (tb != null)
                            {
                                ListView listviewother = tb.Content as ListView;
                                listviewother.ItemsSource = matchlist;
                            }

                        }


                    }
                }
            }
        }
        


    }
}
